package render
{
	import flash.display.BitmapData;
	import flash.display.Stage;
	import flash.display3D.Context3D;
	import flash.display3D.Program3D;
	import flash.display3D.textures.Texture;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import render.materials.shaders.Shader2D;
	import render.materials.textures.TextureSourceType;
	import render.utils.TextureUtil;
	
	public class RenderManager
	{
		/**
		 *舞台对象 
		 */		
		private var _stage:Stage;
		/**
		 *Context3D对象 
		 */		
		private var _context3D:Context3D;
		private var _program3D:Program3D;
		/**
		 *材质贴图库
		 */		
		private var _textureLib:Dictionary=new Dictionary(true);
		
		public function RenderManager()
		{
		}
		
		private static var _instance:RenderManager;
		
		/**
		 *材质贴图库
		 */
		public function get textureLib():Dictionary
		{
			return _textureLib;
		}

		/**
		 * Context3D对象
		 */
		public function get context3D():Context3D
		{
			return _context3D;
		}

		/**
		 * @private
		 */
		public function set context3D(value:Context3D):void
		{
			_context3D = value;
		}

		public function get stage():Stage
		{
			return _stage;
		}
		
		public function set stage(value:Stage):void
		{
			_stage = value;
		}
		
		public static function get instance():RenderManager
		{
			if(_instance == null)
			{
				_instance = new RenderManager();
			}
			return _instance;
		}
		
		/**
		 *创建着色器 
		 * @param context3D
		 * @param materialClass
		 * @param vertexShaderString
		 * @param fragmentShaderString
		 * @return 
		 * 
		 */		
		public function createShader(context3D:Context3D, program3D:Program3D, vertexShaderString:String, fragmentShaderString:String):Shader2D
		{
			return new Shader2D(context3D, program3D, vertexShaderString, fragmentShaderString);
		}
		
		/**
		 * 根据位图创建Texture
		 * @param id ID编号
		 * @param textureSourceType 材质贴图来源类型
		 * @param textureSource 材质贴图来源数据
		 * @return
		 */
		public function createTexture(id:String, textureSourceType:int, textureSource:*):Texture
		{
			if (textureSource == null) return null;
			if (!(id in _textureLib))
			{
				var texture:Texture;
				switch(textureSourceType)
				{
					case TextureSourceType.BITMAPDATA:
						texture = TextureUtil.createTextureByBitmapData(id, _context3D, textureSource as BitmapData);
						break;
					case TextureSourceType.COMPRESSED_BYTEARRAY:
						var byteArray:ByteArray=textureSource as ByteArray;
						texture = TextureUtil.createTextureByCompressedByteArray(id, _context3D, byteArray);
						byteArray.clear();
						byteArray=null;
						break;
					case TextureSourceType.BYTEARRAY:
						byteArray=textureSource as ByteArray;
						texture = TextureUtil.createTextureByByteArray(id, _context3D, byteArray);
						byteArray.clear();
						byteArray=null;
						break;
					case TextureSourceType.SIZE:
						var size:Point=textureSource as Point;
						texture=TextureUtil.createTextureBySize(id, _context3D, size.x, size.y);
						size=null;
						break;
				}
				_textureLib[id]=texture;
			}
			return _textureLib[id] as Texture;
		}
	}
}